//
//  MDAFNetworkRequestmanager.m
//  dingdang_v2
//
//  Created by lin on 16/1/13.
//  Copyright © 2016年 mac mini. All rights reserved.
//

#import "MDAFNetworkRequestmanager.h"

const NSString* baseURL = @"http://app.dd5s.com:8080";

@implementation MDAFNetworkRequestmanager

+ (AFHTTPRequestOperation*)sendPostRequestWithBaseURL:(NSString*)baseURLStr
                                               AddURL:(NSString*)addURLStr
                                               Params:(id)params
                                         SuccessBlock:(MDAFNetWorkRequestSuccessBlock)successBlock
                                            FailBlock:(MDAFNetWorkRequestFailBlock)failBlock
                                           ErrorBlock:(MDAFNetWorkRequestErrorBlock)errorBlock
{
    AFHTTPRequestOperationManager *aFHTTPRequestOperationManager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:baseURLStr]];
    aFHTTPRequestOperationManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    AFHTTPRequestOperation* aFHTTPRequestOperation = [aFHTTPRequestOperationManager POST:addURLStr parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSData* responseData = responseObject;
        //NSString* str = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
        NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil];
        
        NSNumber* successCode = [resDict objectForKey:@"status"];
        if (0 == successCode.intValue)
        {
            successBlock(operation, resDict);
        } else
        {
            failBlock(operation, resDict);
        }
        
    } failure:^(AFHTTPRequestOperation* operation, NSError* error) {
        errorBlock(operation, error);
    }];
    
    return aFHTTPRequestOperation;
}

+ (AFHTTPRequestOperation*)sendPostRequestWithAddURL:(NSString*)addURLStr
                                              Params:(id)params
                                        SuccessBlock:(MDAFNetWorkRequestSuccessBlock)successBlock
                                           FailBlock:(MDAFNetWorkRequestFailBlock)failBlock
                                          ErrorBlock:(MDAFNetWorkRequestErrorBlock)errorBlock
{

    AFHTTPRequestOperationManager *aFHTTPRequestOperationManager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:baseURL]];
    aFHTTPRequestOperationManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    AFHTTPRequestOperation* aFHTTPRequestOperation = [aFHTTPRequestOperationManager POST:addURLStr parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSData* responseData = responseObject;
        //NSString* str = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
        NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil];
        NSNumber* successCode = [resDict objectForKey:@"code"];
        
        if (0 == successCode.intValue)
        {
            successBlock(operation, resDict);
        } else
        {
            failBlock(operation, resDict);
        }
        
        
    } failure:^(AFHTTPRequestOperation* operation, NSError* error) {
        NSLog(@"error occured : %@", error);
        errorBlock(operation, error);
    }];
    
    return aFHTTPRequestOperation;
}

@end
